#include <iostream>
#include <thread>
#include <unistd.h>
#include "smart/smart_net_pubsub.hpp"
#include <string.h>


#define URL "tcp://127.0.0.1:8899"

int server ()
{
    log_debug("------------- server start -------------");
    SmartNetPubsub pub(SmartNetPubsub::NP_PUB, URL);
    pub.init();
    while (true) {
        getchar();
        pub.send_mesg((uint8_t *)"ok.", 3);
    }

    return 0;
}

int client ()
{
    log_debug("------------- client start -------------");
    SmartNetPubsub sub(SmartNetPubsub::NP_SUB, URL);
    sub.init();
    sub.loop_recv_mesg([](uint8_t *buf, uint32_t len){
        log_debug("---------------------------> len:%d.", len, buf);
    });

    return 0;
}

int main(const int argc, const char *argv[])
{   
    int rc;
    log_debug("main");
    if ((argc == 2) && (strcmp (argv[1], "-s") == 0)) {
        rc = server();
    } else if ((argc == 2) && (strcmp (argv[1], "-c") == 0)) {
        rc = client();
    } else {
        fprintf (stderr, "Usage: %s [-s|-c]\n", argv[0]);
        exit (EXIT_FAILURE);
    }
    exit (rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}